package com.anhry.www.safety.manage.enterprise.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.enterprise.bean.TbCorpPerson;
import com.anhry.www.safety.manage.enterprise.service.EnterpriseService;
import com.anhry.www.safety.manage.system.bean.TbUserDept;
import com.anhry.www.safety.util.dao.UtilDao;

/**
 * @date:Jun 19, 2012
 * @remark:
 * @author:dongbin
 */
@Service
@SuppressWarnings("unchecked")
public class EnterpriseServiceImpl implements EnterpriseService{
	
	@Resource
	private UtilDao utilDao;

	public UtilDao getUtilDao() {
		return utilDao;
	}
	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

	public List getCorpList(String hql, String orderBy, int start, int count)throws Exception {
		StringBuffer hqls = new StringBuffer();
		if(count == 0){
			hqls.append("select count(*) from TbCorpInfoView t ").append(hql);
		}else{
			hqls.append("from TbCorpInfoView t ").append(hql).append(orderBy);
		}
		return (List)this.utilDao.getHqlQuery(hqls.toString(), start, count);
	}
	//查询企业安全负责人
	public TbCorpPerson getCorpSafetyHead(Long corpId,String type)throws Exception {
		StringBuffer hqls = new StringBuffer();
			hqls.append("from TbCorpPerson t  where  t.corpId="+corpId+" and t.perType='"+type+"' ");
			List list=(List)this.utilDao.getHqlQuery(hqls.toString());
			if(list!=null&&list.size()>0){
				return (TbCorpPerson)(list.get(0));
			}
		return null;
	}

//	public List getSdList(List deptList, TbUser user) throws Exception {
//		try {
//			List list = new ArrayList();
//			String grade = user.getTbUserGrade().getId().toString();//用户级别
//			if("4".equals(grade) || "6".equals(grade) || "7".equals(grade)){//区行业、属地
//				for(int i=0;i<deptList.size();i++){
//					TbUserDept dept = (TbUserDept)deptList.get(i);
//					if(user.getTbUserUnit().getId().toString().equals(dept.getId().toString())){
//						list.add(dept);
//						break;
//					}
//				}
//				return list;
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return deptList;
//	}

	public TbCorpInfo getCorpByCorpName(String corpId, String corpName)throws Exception {
		StringBuilder hql = new StringBuilder("from TbCorpInfo c where c.isDele=0 and c.corpName = '"+corpName+"'");
		if(null!=corpId && corpId.length()>0){
			hql.append(" and c.id !="+corpId+"");
		}
		List list = (List)this.utilDao.getHqlQuery(hql.toString());
		if(list != null && list.size() > 0){
			return (TbCorpInfo) list.get(0);
		}
		return null;
	}

	public String getGsCode(String corpId, String gsCode) throws Exception {
		StringBuilder hql = new StringBuilder("select count(*) from TbCorpInfo c where c.isDele=0 and c.corpGsCode = '"+gsCode+"'");
		if(null!=corpId && corpId.length()>0){
			hql.append(" and c.id !="+corpId+"");
		}
		return String.valueOf(this.utilDao.getHqlQuerySize(hql.toString()));
	}

	public String getZzCode(String corpId, String zzCode) throws Exception {
		StringBuilder hql = new StringBuilder("select count(*) from TbCorpInfo c where c.isDele=0 and c.corpOrgCode = '"+zzCode+"'");
		if(null!=corpId && corpId.length()>0){
			hql.append(" and c.id !="+corpId+"");
		}
		return String.valueOf(this.utilDao.getHqlQuerySize(hql.toString()));
	}

	public TbCorpInfo getCorpById(Long corpId) throws Exception {
		StringBuilder hql = new StringBuilder("from TbCorpInfo c left join fetch c.tbCorpType left join fetch c.tbCorpTypeMin" +
				" left join fetch c.tbCounty left join fetch c.tbStreet left join fetch c.tbVillage where 1=1 and c.id="+corpId);
		List list = (List)this.utilDao.getHqlQuery(hql.toString());
		if(list != null && list.size() > 0){
			return (TbCorpInfo) list.get(0);
		}
		return null;
	}


}
